/**
 * Created by Eenie on 2016/12/4.
 */

import React, {Component} from 'react';
import {
    View,
    BackAndroid,
    Navigator,
    Image,
    Dimensions,
    InteractionManager,
    ToastAndroid,
    StatusBar
} from 'react-native';

import Splash from './pages/Splash';
import {handleBack} from './common/BackActionUtil';
var _navigator;
var canExit = false;

export  default class InLookApp extends React.Component {

    //默认的构造方法
    constructor(props) {
        super(props);
        this.handleBack = this._handleBack.bind(this);
        this.renderScene = this._renderScene.bind(this);
        this.configureScene = this._configureScene.bind(this);
        this.setStatusBar = this._setStatusBar.bind(this);

        BackAndroid.addEventListener("hardwareBackPress", this.handleBack);

        this.state = {
            statusBarColor: 'transparent',
            statusBarTransparent: true
        };

    }



    // componentDidMount() {
    //     BackAndroid.addEventListener('hardwareBackPress', this.handleBack)
    // }
    //
    // componentWillUnmount() {
    //     BackAndroid.removeEventListener('hardwareBackPress', this.handleBack)
    // }


//处理返回键事件
//     componentDidMount() {
//         const {navigator}=this.props;
//
//     }

    // componentDidMount() {
    //     this.timer = setTimeout(() => {
    //         InteractionManager.runAfterInteractions(() => {
    //             ToastAndroid.show("页面跳转", 1000);
    //         });
    //     }, 2500);
    // }
    //
    // componentWillUnmount() {
    //     this.timer && clearTimeout(this.timer);
    // }


    _handleBack() {
        // return handleBack(_navigator);

        // if (_navigator.getCurrentRoutes().length > 1) {
        //
        //     _navigator.pop();
        //
            return handleBack(_navigator);
        // }else {
        //
        //
        //     if (!canExit) {
        //         ToastAndroid.show("再点一次返回退出应用",2000);
        //         canExit = true;
        //         this.timer = setTimeout(() => {
        //             canExit = false;
        //         }, 2000);
        //
        //
        //     }else {
        //         _navigator.pop();
        //     }
        // }


    }

    render() {
        return (<View style={{flex:1}}>
            <StatusBar
                backgroundColor={this.state.statusBarColor}
                translucent={this.state.statusBarTransparent}/>
            <Navigator
                initialRoute={{
                    component:Splash,
                    title:'splash'
                }}
                renderScene={this.renderScene}
                configureScene={this.configureScene}/>
        </View>);
    }


    //配置页面跳转
    _renderScene(route, navigator) {
        _navigator = navigator;
        let Component = route.component;
        return (<Component {...route} navigator={navigator} />);
    }


    //配置页面跳转的样式：从右边划进来
    _configureScene(route, routeStack) {
        return Navigator.SceneConfigs.PushFromRight;
    }

    _setStatusBar(color, transparent){
        this.setState({
            statusBarColor: color,
            statusBarTransparent: transparent
        })
    }

};

